$().ready( function() {
    
    jQuery.validator.addMethod("dateITA", function(value, element) {
        var check = false;
        var re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
        if( re.test(value)){
            var adata = value.split('/');
            var gg = parseInt(adata[0],10);
            var mm = parseInt(adata[1],10);
            var aaaa = parseInt(adata[2],10);
            var xdata = new Date(aaaa,mm-1,gg);
            if ( ( xdata.getFullYear() == aaaa ) 
                && ( xdata.getMonth () == mm - 1 ) 
                && ( xdata.getDate() == gg ) )
                check = true;
            else
                check = false;
        } else
            check = false;
        return this.optional(element) || check;
    }
    );

    
    jQuery.validator.addMethod( "validReceptionClientCost", function(value) {
        var recepcion = $('#JobCheckReception').attr('checked') == 'checked'?1:0;
        if(recepcion == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validReceptionEmployeeId", function(value) {
        var recepcion = $('#JobCheckReception').attr('checked') == 'checked'?1:0;
        if(recepcion == 1){
            if(value == ''){
                return false;
            }       
        }
        return true;
    }, 'Seleccione un Empleado');
    
    jQuery.validator.addMethod( "validReceptionCostumeId", function(value) {
        var recepcion = $('#JobCheckReception').attr('checked') == 'checked'?1:0;
        if(recepcion == 1){
            if(value == ''){
                return false;
            }       
        }
        return true;
    }, 'Seleccione un Traje');
    
    jQuery.validator.addMethod( "validReceptionEmployeeCost", function(value) {
        var recepcion = $('#JobCheckReception').attr('checked') == 'checked'?1:0;
        if(recepcion == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validServiceServiceId", function(value) {
        var servicio = $('#JobCheckServices').attr('checked') == 'checked'?1:0;
        if(servicio == 1){
            if(value == ''){
                return false;
            }       
        }
        return true;
    }, 'Seleccione un Servicio');
    
    jQuery.validator.addMethod( "validServiceProviderId", function(value) {
        var servicio = $('#JobCheckServices').attr('checked') == 'checked'?1:0;
        if(servicio == 1){
            if(value == ''){
                return false;
            }       
        }
        return true;
    }, 'Seleccione un Proveedor');
    
    jQuery.validator.addMethod( "validServicePrice", function(value) {
        var servicio = $('#JobCheckServices').attr('checked') == 'checked'?1:0;
        if(servicio == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validServiceCost", function(value) {
        var servicio = $('#JobCheckServices').attr('checked') == 'checked'?1:0;
        if(servicio == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
        
    jQuery.validator.addMethod( "validAnimationClientCost", function(value) {
        var animacion = $('#JobCheckAnimation').attr('checked') == 'checked'?1:0;
        if(animacion == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validAnimationEmployeeId", function(value) {
        var animacion = $('#JobCheckAnimation').attr('checked') == 'checked'?1:0;
        if(animacion == 1){
            if(value == ''){
                return false;
            }       
        }
        return true;
    }, 'Seleccione un Empleado');
    
    jQuery.validator.addMethod( "validAnimationEmployeeCost", function(value) {
        var animacion = $('#JobCheckAnimation').attr('checked') == 'checked'?1:0;
        if(animacion == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validAnimationCountInvitedRequired", function(value) {
        var animacion = $('#JobCheckAnimation').attr('checked') == 'checked'?1:0;
        if(animacion == 1){
            if(isNaN(value)){
                return false;
            }       
        }
        return true;
    }, 'Ingrese un valor numérico');
    
            
    jQuery.validator.addMethod( "validShowClientCost", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsShowEmployee][ = 23 caracteres
        var aux = name.substr(23, name.length - 23);
        //ahora aplico un split 
        var datos = aux.split(']');
        var show_id = datos[0];
        var show = $('#JobCheckShows').attr('checked') == 'checked'?1:0;
        var show_specific = $('#JobsShowShowId'+show_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de show, y el check del show en particular
        if(show == 1 && show_specific == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validShowEmployeeId", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsShowEmployee][ = 23 caracteres
        var aux = name.substr(23, name.length - 23);
        //ahora aplico un split 
        var datos = aux.split(']');
        var show_id = datos[0];
        var show = $('#JobCheckShows').attr('checked') == 'checked'?1:0;
        var show_specific = $('#JobsShowShowId'+show_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de show, y el check del show en particular
        if(show == 1 && show_specific == 1){
            if(value == ''){
                return false;
            }       
        }       
        return true;
    }, 'Seleccione un Empleado');
    
    jQuery.validator.addMethod( "validShowCostumeId", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsShowEmployee][ = 23 caracteres
        var aux = name.substr(23, name.length - 23);
        //ahora aplico un split 
        var datos = aux.split(']');
        var show_id = datos[0];
        var show = $('#JobCheckShows').attr('checked') == 'checked'?1:0;
        var show_specific = $('#JobsShowShowId'+show_id).attr('checked') == 'checked'?1:0;
        //si esta chequeado el contenedor grande de show, y el check del show en particular
        if(show == 1 && show_specific == 1){
            if(value == ''){
                return false;
            }       
        }       
        return true;
    }, 'Seleccione un Traje');
    
    jQuery.validator.addMethod( "validShowEmployeeCost", function(value, element) {
        
        var name = element.name;
        //le saco la parte inicial     data[JobsShowEmployee][ = 23 caracteres
        var aux = name.substr(23, name.length - 23);
        //ahora aplico un split 
        var datos = aux.split(']');
        var show_id = datos[0];
        var show = $('#JobCheckShows').attr('checked') == 'checked'?1:0;
        var show_specific = $('#JobsShowShowId'+show_id).attr('checked') == 'checked'?1:0;
        //si esta chequeado el contenedor grande de show, y el check del show en particular
        if(show == 1 && show_specific == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;

    }, 'Ingrese un valor numérico');
    
                
    jQuery.validator.addMethod( "validProductionClientCost", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsProductionEmployee][ = 29 caracteres
        var aux = name.substr(29, name.length - 29);
        //ahora aplico un split 
        var datos = aux.split(']');
        var production_id = datos[0];
        var production = $('#JobCheckProductions').attr('checked') == 'checked'?1:0;
        var production_specific = $('#JobsProductionProductionId'+production_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de la obra, y el check de la obra en particular
        if(production == 1 && production_specific == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;
    }, 'Ingrese un valor numérico');
    
    jQuery.validator.addMethod( "validProductionEmployeeId", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsProductionEmployee][ = 29 caracteres
        var aux = name.substr(29, name.length - 29);
        //ahora aplico un split 
        var datos = aux.split(']');
        var production_id = datos[0];
        var production = $('#JobCheckProductions').attr('checked') == 'checked'?1:0;
        var production_specific = $('#JobsProductionProductionId'+production_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de la obra, y el check de la obra en particular
        if(production == 1 && production_specific == 1){
            if(value == ''){
                return false;
            }       
        }       
        return true;
    }, 'Seleccione un Empleado');
    
    jQuery.validator.addMethod( "validProductionCostumeId", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsProductionEmployee][ = 29 caracteres
        var aux = name.substr(29, name.length - 29);
        //ahora aplico un split 
        var datos = aux.split(']');
        var production_id = datos[0];
        var production = $('#JobCheckProductions').attr('checked') == 'checked'?1:0;
        var production_specific = $('#JobsProductionProductionId'+production_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de la obra, y el check de la obra en particular
        if(production == 1 && production_specific == 1){
            if(value == ''){
                return false;
            }       
        }       
        return true;
    }, 'Seleccione un Traje');
    
    jQuery.validator.addMethod( "validProductionEmployeeCost", function(value, element) {
        var name = element.name;
        //le saco la parte inicial     data[JobsProductionEmployee][ = 29 caracteres
        var aux = name.substr(29, name.length - 29);
        //ahora aplico un split 
        var datos = aux.split(']');
        var production_id = datos[0];
        var production = $('#JobCheckProductions').attr('checked') == 'checked'?1:0;
        var production_specific = $('#JobsProductionProductionId'+production_id).attr('checked') == 'checked'?1:0;
        
        //si esta chequeado el contenedor grande de la obra, y el check de la obra en particular
        if(production == 1 && production_specific == 1){
            if(value == '' || isNaN(value)){
                return false;
            }       
        }       
        return true;

    }, 'Ingrese un valor numérico');
    
   
    jQuery.validator.addMethod( "validEventNameRequired", function(value, element) {
        var tipo = $('#JobType').val();
        if(tipo == 'event'){
            if(value == ''){
                return false;
            }       
        }
        return true;
    });

    jQuery.validator.addMethod( "validDateFrom", function(value) {
        //tomo las horas y los minutos
        var hora_inicio = $('#JobStartHour').val();
        var hora_fin = $('#JobFinishHour').val();
        var minuto_inicio = $('#JobStartMinute').val();
        var minuto_fin = $('#JobFinishMinute').val();
        
        if(minuto_inicio < 10){
            minuto_inicio = '0'+minuto_inicio;
        }
        if(minuto_fin < 10){
            minuto_fin = '0'+minuto_fin;
        }
        
        var inicio = hora_inicio+''+minuto_inicio;
        var fin = hora_fin+''+minuto_fin;
       
        if(parseFloat(inicio) >= parseFloat(fin)){
            return false;
        }       
        return true;
    });

    $("#JobAddForm").validate( {
        rules: {
            "data[Job][pricetype_id]": {
                required: true
            },
            "data[Job][event_name]": {
                validEventNameRequired: true
            },
            "data[Job][count_invited]": {
                required: false,
                number: true
            },
            "data[Job][address]": {
                required: true,
                rangelength: [3,100]
            },
            "data[Job][date_from]": {
                required: true,
                dateITA: true,
                validDateFrom: true
            },
            "data[Job][age]": {
                required: false,
                number: true
            },
            "data[Job][sign]": {
                required: false,
                number: true
            }
        },
        messages: {
            "data[Job][pricetype_id]": {
                required: "<?php __('Seleccione un Tipo de Precio') ?>"
            },
            "data[Job][event_name]": {
                validEventNameRequired: "<?php __('Ingrese el nombre') ?>"
            },
            "data[Job][address]": {
                required: "<?php __('Ingrese la dirección') ?>",
                rangelength: "<?php __('Ingrese entre 3 y 100 caracteres') ?>"
            },
            "data[Job][date_from]": {
                required: "<?php __('Ingrese la fecha de inicio') ?>",
                dateITA: "<?php __('Ingrese una fecha válida') ?>",
                validDateFrom: "<?php __('El horario de fin debe ser mayor que el de inicio') ?>"
            },
            "data[Job][count_invited]": {
                number: "<?php __('Ingrese un valor numérico') ?>"
            },
            "data[Job][age]": {
                number: "<?php __('Ingrese un valor numérico') ?>"
            },
            "data[Job][sign]": {
                number: "<?php __('Ingrese un valor numérico') ?>"
            }
        }

    } );
} );